PHP-Tester
A PHP testing lib that works through both web & cli
Features
- Output tests to HTML:
- Wrapped in
<details>
tag for vanilla-html expand/collapse - Pass/Fail/Success (colorized)
- Any echo/printed content from each test is shown
- Exceptions in tests are caught & printed
- Source is well-formatted so you can view-source OR view it in a browser
- Wrapped in
- Extend the tester class & every method starting with
test
will be run as a test
TODO
Priority TODO
- Add functional testing option
-
$t = new \Taeluf\Tester(); $t->startTest(); doSomeCode(); $t->endTest('name');
-
- Add basic test benchmarking
- Add equality testing function
-
$succeeded = $this->test('expectedOutput', $actualOutput);
- print
"target: ..."
&"actual: ..."
- Handle printing of strings & arrays, maybe other stuff. Maybe just var_dump?? (ugly)
-
Maybe Eventually TODO
- Pick a better name
- Self-promo, maybe? Document it on my website & direct folks there, share my twitter handle, patreon, etc
- Add sub-tests (not sure what I mean here?? But it's in my notes!)
- Store test code in arrays & output it in the HTML for each test
- Use debug_backtrace() or tokenizing or something to get the line of each testFunction & just copy the code from the function & include it in the HTML output
- This should DEFINITELY be optional
Example (OOP Style)
We'll create a simple "translator" function & test it with the OOP approach.
"translation" function:
function translateGreeting($language){
switch ($language){
case "spanish":return "Hola";
case "german": return "Hallo";
case "english": return "Hello";
case "french": return "Bonjour";
default: return null;
}
}
"Tester" class:
class TranslatorTester extends \Taeluf\Tester{
//every function that starts with `test` will be run as a test
public function testTranslateSpanish(){
$actual = translateGreeting('spanish');
$target = "Hola";
echo "Target:{$target}\nActual:{$actual}";
if ($target==$actual)return true;
}
public function prepare(){
//this code runs BEFORE any of your test code
}
//You can declare other functions that DON'T start with `test` if you need helper functions.
}
//TranslatorTester::runAll() // just print the results
//TranslatorTester::run(['testTranslateSpanish']) // run every test method given in that array
// (new TranslatorTester())->run(); //runs all this way, or pass an array of methods to only run those tests
TranslatorTester::runAllToFile(__FILE__.'.html',$alsoPrint=true);
TranslatorTester::xdotoolRefreshFirefox(); // If you have xdotool installed, this will switch to Firefox & refresh focused webpage.
Output:
You can copy the path to the html file & paste it in your browser bar to view your test result
HTML Code
<details>
<summary><b>TranslateSpanish:</b> <span style="color:green;">success</span> </summary>
<div style='padding-left:4ch;white-space:pre;'>
Target:Hola
Actual:Hola
</div>
</details>
Visual
Markdown strips the inline styles, so it lacks the colors & white-space:pre
TranslateSpanish: success
Target:Hola
Actual:Hola
Contribution
PHP code is in 5-src
dir. Put tests in 8-build/test/
. Probably use a sub-directory. Look at my Translator test and see Docu Bear to learn how to document. Orr... just write some code & I'll probably document it up.
- Pick any
TODO
item (or issue if there are any) & submit a pull request that solves just that single item. - Submit issues for feature requests or bug reports!
- See something else? Submit an issue & then submit a pull request that resolves it.